﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;

namespace WindowsFormsApplication1
{
    public class PatientDB
    {
        static string PATH2 = ".//..//..//patient.dbf";

        static public List<Patient> GetPatients()
        {
            ArrayList lines = new ArrayList(File.ReadAllLines(PATH2));
            List<Patient> res = new List<Patient>();

            foreach (var elem in lines)
            {
                Patient p = Patient.parseFromDb((string)elem);
                res.Add(p);
            }

            return res;
        }

        public static int GetNewId(List<Patient> lines)
        {
            int newId;
            if (lines.Count > 0)
            {
                Patient lastPat = (Patient)lines[lines.Count - 1];
                newId = lastPat.id.Value + 1;
            }
            else
            {
                newId = 1;
            }
            return newId;
        }

        public static void SavePatientToDB(Patient patNew)
        {
            List<Patient> lines = GetPatients();
            int newId = GetNewId(lines);
            patNew.id = newId;
            SavePatientToDB(patNew, lines);
        }

        public static void SavePatientToDB(Patient pNew, List<Patient> lines)
        {
            File.WriteAllText(PATH2, "");

            Patient curPat;
            bool patientFound = false;
            int i = 0;

            while (i < lines.Count)
            {
                curPat = (Patient)lines[i];
                if (curPat.id == pNew.id)
                {
                    File.AppendAllText(PATH2, pNew.ToDbFormat() + Environment.NewLine);
                    patientFound = true;
                    i++;
                }
                else
                {
                    File.AppendAllText(PATH2, curPat.ToDbFormat() + Environment.NewLine);
                    i++;
                }
            }
            if (patientFound == false)
            {
                File.AppendAllText(PATH2, pNew.ToDbFormat() + Environment.NewLine);
            }

        }

        public static void EditedPatientToDB(Patient patEdited)
        {
            List<Patient> res = GetPatients();
            File.WriteAllText(PATH2, "");
            for (int i = 0; i < res.Count; i++)
            {
                Patient pat = res[i];
                if (res[i].id != patEdited.id)
                {
                    File.AppendAllText(PATH2, pat.ToDbFormat() + Environment.NewLine);
                }
                else
                {
                    File.AppendAllText(PATH2, patEdited.ToDbFormat() + Environment.NewLine);
                }
            }
        }

        public static bool DeletePatient(Patient pToDel)
        {
            List<Patient> lines = GetPatients();
            File.WriteAllText(PATH2, "");
            bool ifDeleted = false;
            foreach (Patient elem in lines)
            {
                if (elem.id != pToDel.id)
                {
                    File.AppendAllText(PATH2, elem.ToDbFormat() + Environment.NewLine);
                }
                else
                {
                    ifDeleted = true;
                }
            }
            return ifDeleted;
        }


        static public bool TryParseID(string id)
        {
            bool valid = false;
            int number;
            if (Int32.TryParse(id, out number) == true)
            {
                valid = true;
            }
            return valid;
        }

        /* public static void DeleteDPatient(int index)
         {
             List<Patient> patList = GetPatients();
             File.WriteAllText(PATH2, "");
             for (int i = 0; i < patList.Count; i++)
             {
                 Patient p = (Patient)patList[i];
                 if (i != index)
                 {
                     File.AppendAllText(PATH2, p.ToDbFormat() + Environment.NewLine);
                 }
             }

         }*/

        public static void DeleteDPatient(Patient pat)
        {
            List<Patient> patList = GetPatients();
            File.WriteAllText(PATH2, "");
            foreach (Patient el in patList)
            {
                if (el.id.Value != pat.id.Value)
                {
                    File.AppendAllText(PATH2, el.ToDbFormat() + Environment.NewLine);
                }
            }
        }




        public static void EditedPatientToDB(int index, Patient editedPat)
        {
            List<Patient> res = GetPatients();
            File.WriteAllText(PATH2, "");
            for (int i = 0; i < res.Count; i++)
            {
                if (i != index)
                {
                    File.AppendAllText(PATH2, res[i].ToDbFormat() + Environment.NewLine);
                }
                else
                {
                    File.AppendAllText(PATH2, editedPat.ToDbFormat() + Environment.NewLine);
                }
            }
        }
    }
}
